<?php
/**
 * Created by PhpStorm.
 * Author: Lin
 * Date: 2017/12/22
 * Time: 11:24
 */

namespace app\common\exception;

use app\admin\auth\Auth;
use Exception;
use think\App;
use think\Config;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\Request;
use think\Session;

class Http extends Handle
{
    public function render(Exception $e)
    {
        $user_id = Session::get('user.user_id');
        if ($user_id == 1) {
            //如果是开发者则开启debug模式
            App::$debug = true;
            return parent::render($e);
        }
        // 参数验证错误
        if ($e instanceof ValidateException) {
            return json($e->getError(), 422);
        }

        if ((Request::instance()->isAjax())) {
            return json(['status' => false, 'msg' => '500']);
        }

        // 请求异常
        if ($e instanceof HttpException && request()->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
        }

        //可以在此交由系统处理
        return parent::render($e);
    }

}